package com.water.camera.mark;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.plata.base.aclr.utils.SPHelper;
import com.water.camera.App;
import com.water.camera.activity.EditMarkActivity;
import com.water.camera.bean.EditItem;
import com.water.camera.it.EditListener;
import com.water.camera.util.Utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

public abstract class BaseMark extends FrameLayout implements EditListener {

    protected boolean touchable = false;
    protected final int mType;

    public BaseMark(@NonNull Context context) {
        super(context);
        mType = 0;
        init(0);
    }

    public BaseMark(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mType = 1;
        init(1);
    }

    public BaseMark(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mType = 2;
        init(2);
    }

    public BaseMark(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mType = 3;
        init(3);
    }

    protected abstract void init(int type);

    protected void hideView(View... views) {
        for (View v : views) {
            v.setVisibility(View.GONE);
        }
    }

    protected void showView(View... views) {
        for (View v : views) {
            v.setVisibility(View.VISIBLE);
        }
    }

    public void setScale(float f) {
        setScaleX(f);
        setScaleY(f);
    }

    public float getScale() {
        return getScaleX();
    }

    public ArrayList<EditItem> getEditItems() {
        return new ArrayList<>();
    }

    @Override
    public void onShowChange(EditItem item) {
        View view = getChildAt(0).findViewWithTag(item.key);
        if (view == null) {
            return;
        }
        view.setVisibility(item.show ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onValueChange(EditItem item) {
        View view = getChildAt(0).findViewWithTag(item.key + "_value");
        if (view instanceof TextView == false) {
            view = getChildAt(0).findViewWithTag(item.key);
            if (view instanceof TextView) {
                if (item.isPoint()) {
                    ((TextView) view).setText(item.getStringPoint());
                } else {
                    ((TextView) view).setText(item.value);
                }
            }
        } else {
            if (item.isPoint()) {
                ((TextView) view).setText(item.getStringPoint());
            } else {
                ((TextView) view).setText(item.value);
            }
        }
    }

    @CallSuper
    public void update() {
        if (isDefault()) {
            return;
        }
        for (EditItem item : getEditItems()) {
            onShowChange(item);
        }
        String key = getClass().getSimpleName() + "_scale";
        float scale = SPHelper.getInstance(getContext()).getFloat(key, 1.0f);
        if (touchable) {
            setScale(scale);
        } else if (getParent() instanceof View) {
            View v = (View) getParent();
            v.setScaleY(scale);
            v.setScaleX(scale);
        }
    }

    protected boolean setTime(TextView tv, SimpleDateFormat format) {
        long time = getTime();
        if (time > 0) {
            tv.setText(format.format(time));
            return true;
        }
        return false;
    }

    protected long getTime() {
        String key = getTimeKey();
        if (TextUtils.isEmpty(key)) {
            String tmp = getClass().getName().substring("com.water.camera.mark.Mark".length());
            key = "time" + tmp + "_time";
        } else {
            key = key + "_time";
        }
        long time = SPHelper.getInstance(App.getApp()).getLong(key, 0);
        return time;
    }

    protected boolean setWeek(TextView tv) {
        return setWeek(tv, 0);
    }

    protected boolean setWeek(TextView tv, int type) {
        String key = getTimeKey();
        if (TextUtils.isEmpty(key)) {
            String tmp = getClass().getName().substring("com.water.camera.mark.Mark".length());
            key = "time" + tmp + "_time";
        } else {
            key = key + "_time";
        }
        long time = SPHelper.getInstance(App.getApp()).getLong(key, 0);
        if (time > 0) {
            tv.setText(Utils.dateToWeek(time, type));
            return true;
        }
        return false;
    }

    protected String getTimeKey() {
        return null;
    }


    float downX = 0, downY = 0;
    boolean isMove = false;
    private int mLastX;
    private int mLastY;

    public void setTouchable(boolean bo) {
        touchable = bo;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!touchable) {
            return super.onTouchEvent(event);
        }
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        //动画实现移动代码
        //--------------------------------------------------
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getRawX();
                downY = event.getRawY();
                isMove = false;
                break;
            case MotionEvent.ACTION_MOVE:
                int touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
                if (!isMove && Math.abs(event.getRawX() - downX) <= touchSlop && Math.abs(event.getRawY() - downY) < touchSlop) {
                    ;
                } else {
                    isMove = true;
                }
                int delaltax = x - mLastX;
                int delaltaY = y - mLastY;
                float tranX = getTranslationX() + delaltax;
                float tranY = getTranslationY() + delaltaY;
                setTranslationX(tranX);
                setTranslationY(tranY);
                break;
            case MotionEvent.ACTION_UP:
                if (!isMove) {
                    onClick();
                }
                break;
            default:
                break;
        }
        mLastX = x;
        mLastY = y;
        return true;
    }

    private void onClick() {
        String name = getClass().getName();
        Intent i = new Intent(getContext(), EditMarkActivity.class);
        i.putExtra("name", name);
        ((Activity) getContext()).startActivityForResult(i, 102);
    }

    public boolean isFullScreen() {
        return false;
    }

    public boolean isFullWidth() {
        return false;
    }

    private boolean isDefault() {
        return mType == 0;
    }

    protected void setText(TextView tv, String key, View... group) {
        Utils.setText(tv, key, isDefault(), group);
    }

    protected void setText(TextView tv, String key, String str, View... group) {
        Utils.setText(tv, key, str, isDefault(), group);
    }
}
